package com.ruoyi.web.controller.business.sundry;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbPerformance;
import com.ruoyi.business.sundry.domain.vo.JbPerformanceVo;
import com.ruoyi.business.sundry.domain.vo.JbUserCommissionVo;
import com.ruoyi.business.sundry.domain.vo.JbUserPerformanceVo;
import com.ruoyi.business.sundry.service.JbPerformanceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 绩效 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("绩效")
@RestController
@RequestMapping("/performance")
public class JbPerformanceController extends BaseController
{
    @Autowired
    private JbPerformanceService service;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询绩效列表
     */
    @ApiOperation("查询绩效列表")
    @PreAuthorize("@ss.hasAnyPermi('performance:list,performance:deptList,performance:companyList')")
    @GetMapping("/listPage")
    public TableDataInfo listPage(PageParam page, JbPerformance jbPerformance)
    {
        return JoR.getDataTable(service.listPage(page, jbPerformance));
    }

    /**
     * 导出绩效列表
     */
    @ApiOperation("导出绩效列表")
    @PreAuthorize("@ss.hasPermi('performance:export')")
    @Log(title = "绩效", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbPerformance jbPerformance)
    {
        LambdaQueryWrapper<JbPerformance> select = new LambdaQueryWrapper<>();
        IPage<JbPerformance> iPage = service.page(page.b(), select);

        List<JbPerformance> list = iPage.getRecords();
        ExcelUtil<JbPerformance> util = new ExcelUtil<JbPerformance>(JbPerformance.class);
        util.exportExcel(response, list, "绩效数据");
    }

    /**
     * 获取绩效详细信息
     */
    @ApiOperation("获取绩效详细信息")
    @PreAuthorize("@ss.hasPermi('performance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增绩效
     */
    @ApiOperation("新增绩效")
    @PreAuthorize("@ss.hasPermi('performance:add')")
    @Log(title = "绩效", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbPerformanceVo jbPerformance)
    {
        return toAjax(service.saveInfo(jbPerformance));
    }

    /**
     * 修改绩效
     */
    @ApiOperation("修改绩效")
    @PreAuthorize("@ss.hasPermi('performance:edit')")
    @Log(title = "绩效", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbPerformance jbPerformance)
    {
        if (ObjectUtils.isNotEmpty(jbPerformance.getRelatedId()))
        {
            SysUser sysUser = sysUserService.selectUserById(jbPerformance.getRelatedId());
            jbPerformance.setDepartment(ObjectUtils.isEmpty(sysUser) ? 0l : sysUser.getDeptId());
        }
        return toAjax(service.updateById(jbPerformance));
    }

    /**
     * 删除绩效
     */
    @ApiOperation("删除绩效")
    @PreAuthorize("@ss.hasPermi('performance:remove')")
    @Log(title = "绩效", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 获取目标总和
     */
    @ApiOperation("获取目标总和")
    @PreAuthorize("@ss.hasPermi('performance:query')")
    @GetMapping(value = "/getTargetTotal")
    public AjaxResult getTargetTotal(JbPerformance jbPerformance)
    {
        return success(service.getTargetTotal(jbPerformance));
    }

    @ApiOperation("批量修改绩效")
    @PreAuthorize("@ss.hasPermi('performance:edit')")
    @Log(title = "绩效", businessType = BusinessType.UPDATE)
    @PutMapping("/editBatch")
    public AjaxResult editBatch(@RequestBody JbPerformanceVo jbPerformance)
    {
        JbPerformance performance = BeanUtil.toBean(jbPerformance, JbPerformance.class);
        return toAjax(service.update(performance, Wrappers.lambdaUpdate(JbPerformance.class)
                .in(JbPerformance::getId, jbPerformance.getIds())));
    }

    /**
     * 查询用户绩效列表
     */
    @ApiOperation("查询用户绩效列表")
//    @PreAuthorize("@ss.hasAnyPermi('performance:userPerformancePage')")
    @GetMapping("/userPerformancePage")
    public TableDataInfo userPerformancePage(PageParam page, JbUserPerformanceVo jbPerformance)
    {
        return JoR.getDataTable(service.userPerformancePage(page, jbPerformance));
    }

    /**
     * 导出查询用户绩效列表
     */
    @ApiOperation("导出查询用户绩效列表")
    @PostMapping("/exportUserPerformance")
    public void exportUserPerformance(HttpServletResponse response, JbUserPerformanceVo jbPerformance)
    {
        List<JbUserPerformanceVo> list = service.exportUserPerformance(jbPerformance);
        ExcelUtil<JbUserPerformanceVo> util = new ExcelUtil<JbUserPerformanceVo>(JbUserPerformanceVo.class);
        util.exportExcel(response, list, "员工绩效导出");
    }

    /**
     * 查询用户提成列表
     */
    @ApiOperation("查询用户提成列表")
//    @PreAuthorize("@ss.hasAnyPermi('performance:userPerformancePage')")
    @GetMapping("/userCommissionPage")
    public TableDataInfo userCommissionPage(PageParam page, JbUserCommissionVo jbPerformance)
    {
        return JoR.getDataTable(service.userCommissionPage(page, jbPerformance));
    }
    /**
     * 查询用户提成列表
     */
    @ApiOperation("查询用户提成列表")
//    @PreAuthorize("@ss.hasAnyPermi('performance:userPerformancePage')")
    @GetMapping("/userCommissionPageSum")
    public AjaxResult userCommissionPageSum(PageParam page, JbUserCommissionVo jbPerformance)
    {
        return success(service.userCommissionPageSum(jbPerformance));
    }

    /**
     * 导出查询用户绩效列表
     */
    @ApiOperation("导出查询用户绩效列表")
    @PostMapping("/exportUserCommission")
    public void exportUserCommission(HttpServletResponse response, JbUserCommissionVo jbPerformance)
    {
        List<JbUserCommissionVo> list = service.exportCommission(jbPerformance);
        ExcelUtil<JbUserCommissionVo> util = new ExcelUtil<JbUserCommissionVo>(JbUserCommissionVo.class);
        util.exportExcel(response, list, "员工提成导出");
    }
}
